package dao.impl;

import bean.laundry;
import dao.laundryDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.c3p0Utils;

import java.sql.SQLException;
import java.util.List;

public class laundryDaoImpl implements laundryDao {
    @Override
    public int laundryAdd(laundry l,String date) {
        String sql = "INSERT INTO LaundryList VALUES (null,?,?,?,?,?,?)";
        QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
        int i = 0;
        try {

            i = runner.update(sql,l.getMerchId(),l.getUid(),date,l.getSalessum(),l.getShouldmoney(),l.getRealitymoney());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public List<laundry> showLanudry() {
        String sql = "select laundryId,LaundryList.merchId,merchname,salestime,salessum,shouldmoney,realitymoney from LaundryList,Merch where LaundryList.merchId=Merch.merchId";
        QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
        List<laundry> list = null;
        try {
             list = runner.query(sql, new BeanListHandler<laundry>(laundry.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<laundry> LaundrySearch(int laundryId) {
        String sql = "select laundryId,LaundryList.merchId,merchname,salestime,salessum,shouldmoney,realitymoney from LaundryList,Merch where LaundryList.merchId=Merch.merchId AND laundryId=?";
        QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
        List<laundry> l= null;
        try {
            l = runner.query(sql,new BeanListHandler<laundry>(laundry.class),laundryId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return l;
    }

    @Override
    public double getprice(int id) {
        QueryRunner runner = new QueryRunner(c3p0Utils.getDataSource());
        String sql = "SELECT merchprice FROM Merch WHERE merchId = ?";
        double price = 0;
        try {
            price = (Double) runner.query(sql,new ScalarHandler<>(),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return price;
    }

}
